package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.Font;
import javax.swing.JSeparator;
import javax.swing.ImageIcon;

import controllers.OBLSettings;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class LoginUI extends JPanel 
{

	private static final long serialVersionUID = 1L;
	
	private JLabel idLabel = null;
	private JLabel pNameLabel = null;
	private JLabel lblPassword;
	private JLabel lNameLabel = null;
	private JLabel lblUser;
	private JLabel facLabel = null;
	JPasswordField txtPassword = null;
	JTextField txtUser = null;
	public JButton btnBack = null;
	JButton btnLogin;
	private JLabel lblEnterUserName;
	JCheckBox chckbxShowPass;
	JButton btnSearchBooks;
	 

	
	/**
	 * This is the default constructor
	 * @param welcomeUI 
	 */
	public LoginUI() 
	{
		super();
		initialize();
	}

	/**
	 * This method initializes LoginPanel
	 */
	private void initialize()
	{
		btnBack = new JButton("Back");
		btnBack.setFont(OBLSettings.buttonMenuTextFont);
		
		lblUser = new JLabel();
		lblUser.setFont(OBLSettings.labelFont);
		lblUser.setBounds(new Rectangle(99, 156, 93, 28));//15, 74, 113, 18
		lblUser.setText("ID");
		
		lblPassword = new JLabel();
		lblPassword.setFont(OBLSettings.labelFont);
		lblPassword.setBounds(new Rectangle(99, 195, 100, 28)); // 15, 17, 113, 17
		lblPassword.setText("Password");
		
		this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		this.setLayout(null);
		this.add(lblPassword, null);
		this.add(lblUser, null);
		((JPasswordField) getTxtPassword()).setEchoChar("\u25CF".toCharArray()[0]);//set bullet char to password field
		this.add(getTxtPassword(), null);
		this.add(getTxtUser(), null);
		
		btnBack.setBounds(21, 11, 83, 41);
		this.add(btnBack);
		btnLogin = new JButton("Login");		
		btnLogin.setFont(OBLSettings.buttonTextFont);
		btnLogin.setBounds(285, 260, 106, 41);
		add(btnLogin);
		
		lblEnterUserName = new JLabel("Enter ID and Password");
		lblEnterUserName.setFont(OBLSettings.labelFont);
		lblEnterUserName.setBounds(9, 95, 233, 34);
		add(lblEnterUserName);
		
		chckbxShowPass = new JCheckBox("show password");
		chckbxShowPass.setOpaque(false);
		chckbxShowPass.setBackground(Color.WHITE);
		chckbxShowPass.setFont(OBLSettings.labelFont);
		chckbxShowPass.setBounds(99, 233, 157, 28);
		add(chckbxShowPass);
		
		btnSearchBooks = new JButton("Search Books");
		btnSearchBooks.setFont(OBLSettings.buttonTextFont);
		btnSearchBooks.setBounds(641, 69, 149, 41);
		add(btnSearchBooks);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 57, 855, 30);
		add(separator);
		
		JLabel lblOblLogin = new JLabel("OBL Login");
		lblOblLogin.setFont(OBLSettings.headlinesTextFont);
		lblOblLogin.setBounds(331, 11, 137, 41);
		add(lblOblLogin);
		
		JLabel background = new JLabel("");
		
		background.setIcon(OBLSettings.IMG_CLIENT_LOGIN_BACKGROUND);
		background.setBounds(0, 57, 800, 543);
		add(background);
		
		
		txtPassword.addActionListener(new ActionListener()
		{
	        public void actionPerformed(ActionEvent e)
	        {
	                btnLogin.doClick();
	        }  
	    });
		
	
	}//init

	/**
	 * This method initializes pNameTextField	
	 */
	private JTextField getTxtPassword() 
	{
		if (txtPassword == null)
		{
			txtPassword = new JPasswordField();
			txtPassword.setFont(OBLSettings.textFont);
			txtPassword.setBounds(new Rectangle(217, 197, 227, 28)); // 133, 45, 127, 17
			txtPassword.addFocusListener(new java.awt.event.FocusAdapter() 
			{
				public void focusLost(java.awt.event.FocusEvent e) 
				{ 
					String pName = txtPassword.getText();
				}
		    });
		}
		return txtPassword;
	}

	/**
	 * This method initializes lNameTextField	
	 */
	private JTextField getTxtUser()
	{
		if (txtUser == null) 
		{
			txtUser = new JTextField();
			txtUser.setFont(OBLSettings.textFont);
			txtUser.setBounds(new Rectangle(217, 158, 227, 28));
		
			txtUser.addFocusListener(new java.awt.event.FocusAdapter() 
			{
			public void focusLost(java.awt.event.FocusEvent e) 
			  {
				String lName=txtUser.getText();
			  }
			});
	   }

	return txtUser;
	}
	

	
	
	
	public String getUser()
	{
		return txtUser.getText();
	}
	
	public String getPass()
	{
		return txtPassword.getText();
	}
	
	public void setUser(String username) 
	{
		this.txtUser.setText(username); 
	}
	public void setPass(String password)
	{
		this.txtPassword.setText(password);
	}
	
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		
	}
	
}